#include"Command.hpp"
#include"Log.hpp"
#include"InetAddr.hpp"
#include<iostream>
#include<string>
using namespace LogModule;
void Usage(char* usage)
{
    std::cerr<<usage<<"ip port";
}
int main(int argc,char* argv[])
{
    if(argc != 3)
    {
        Usage(argv[0]);
        exit(USAGE_ERR);
    }
    int sockfd=socket(AF_INET,SOCK_STREAM,0);
    if(sockfd<0)
    {
        LOG(LogLevel::ERROR)<<"sock error";
        exit(SOCK_ERR);
    }
    std::string serveip=argv[1];
    uint16_t serveport=std::stoi(argv[2]);
    InetAddr serveaddr(serveip,serveport);
    int n=connect(sockfd,serveaddr.NetAddrPtr(),serveaddr.Len());
    if(n<0)
    {
        LOG(LogLevel::ERROR)<<"connect error";
        exit(CONNECT_ERR);
    }
    while(true)
    {
        std::string line;
        std::cout<<"please enter@ ";
        getline(std::cin,line);
        write(sockfd,line.c_str(),line.size());

        char buffer[1024];
        int m=read(sockfd,buffer,sizeof(buffer));
        if(m>0)
        {
            buffer[m]=0;
            std::cout<<"serve echo: "<<buffer<<std::endl;
        }

    }
    return 0;
}